#include <QtGui/QApplication>
#include "frm_exercises.h"
#include "ui_frm_exercises.h"
#include "frm_menu.h"
#include "frm_routine_menu.h"
#include <QDesktopWidget>
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QMessageBox>



frm_exercises::frm_exercises(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::frm_exercises)
{
    ui->setupUi(this);
    setWindowFlags( Qt:: WindowTitleHint | Qt:: CustomizeWindowHint | Qt:: WindowMinimizeButtonHint | Qt:: WindowMaximizeButtonHint);
    this->move(QApplication::desktop()->screen()->rect().center() - this->rect().center());
    //Cargamos el fichero exercises en la lista 1
    loadTextFile(1, "exercises.txt");
    //Comprobamos si existe rutina
    if( comprobar() == 1)
    {
        loadTextFile(2, "exMonday.txt");

    }

}

frm_exercises::~frm_exercises()
{
    delete ui;
}


void frm_exercises::on_btnNext_clicked()
{
    //comprobamos si es el ultimo boton
    if (ui->label->text() == "Sunday")
    {
        frm_routine_menu *routine_menu = new frm_routine_menu();
        this->hide();
        routine_menu->show();
    }


    //Colocamos el dia correspondiente y guardamos fichero
    if (ui->label->text() == "Monday"){


        saveTextFile("exMonday.txt");

        ui->label->setText("Tuesday");
        modulo("exTuesday.txt");


    }else if (ui->label->text() == "Tuesday"){

        saveTextFile("exTuesday.txt");
        ui->label->setText("Wednesday");

        modulo("exWednesday.txt");


    }else if (ui->label->text() == "Wednesday"){

        saveTextFile("exWednesday.txt");
        ui->label->setText("Thursday");

        modulo("exThursday.txt");

    }else if (ui->label->text() == "Thursday"){

        saveTextFile("exThursday.txt");
        ui->label->setText("Friday");

        modulo("exFriday.txt");


    }else if (ui->label->text() == "Friday"){

        saveTextFile("exFriday.txt");
        ui->label->setText("Saturday");

        modulo("exSaturday.txt");

    }else if (ui->label->text() == "Saturday"){

        saveTextFile("exSaturday.txt");
        ui->label->setText("Sunday");
        ui->btnNext->setIcon(QIcon(":/Images/btn_finish.png"));
        //ui->btnNext->setStyleSheet("background-image: url(:/Images/btn_finish.png)");
        modulo("exSunday.txt");

    }else if(ui->label->text() == "Sunday"){

        saveTextFile("exSunday.txt");
        modificar(1);

    }

}


void frm_exercises::on_btnAnnadir_clicked()
{
        for (int i =0; i<ui->listWidget->count(); i++){
            if(ui->listWidget->item(i)->isSelected())
                ui->listWidget_2->addItem(ui->listWidget->item(i)->text());
        }

}


void frm_exercises::on_btnEliminar_clicked()
{
    for (int i =0; i<ui->listWidget_2->count(); i++){
        if(ui->listWidget_2->item(i)->isSelected()){
            ui->listWidget_2->removeItemWidget(ui->listWidget_2->takeItem(i));
        }

    }

}

void frm_exercises::saveTextFile(QString NameFile)
{
  QFile file(NameFile);
  if(file.open(QIODevice::WriteOnly | QIODevice::Truncate))
  {
    QTextStream text_stream_for_writing(&file);

    //volcamos la lista al fichero
    for (int i =0; i!= ui->listWidget_2->count(); i++)
    text_stream_for_writing << ui->listWidget_2->item(i)->text() << "\n";

    file.close();
  }else
      qFatal("No puedo abrir el fichero para escritura.");


}

void frm_exercises::loadTextFile(int list, QString NameFile)
 {
        QString f;
        QString i;

        QFile file(NameFile);

            if (file.open(QIODevice::ReadWrite| QIODevice::Text))
            {
                QTextStream in(&file);

                while(!in.atEnd())
                {
                  f = in.readLine(); // leemos línea
                  if (list == 2)
                      ui->listWidget_2->addItem(f);
                  else{
                      QListWidgetItem *item = new QListWidgetItem(f);
                      i = in.readLine(); //leemos imagen
                      item->setIcon( QIcon(i));
                      ui->listWidget->addItem(item);
                  }

                  //item->setIcon( QIcon(i));
                  //ui->listWidget->insertItem(0, item);
                }
            }else
                qFatal("No puedo abrir el fichero para lectura.");
}



void modificar(int a)
{

    QFile file("routine.txt");
    if(file.open(QIODevice::WriteOnly | QIODevice::Truncate))
    {
      QTextStream text_stream_for_writing(&file);

      text_stream_for_writing << a ;

      file.close();
    }else
        qFatal("No puedo abrir el fichero para escritura.");
}

void frm_exercises::modulo(QString Name)
{
    ui->listWidget_2->clear();

    if(comprobar()==1)
    {
      loadTextFile(2, Name);
    }
    else
    {
      ui->listWidget_2->clear();
    }
}
